package com.Arsanesia.TempleRush.Views.Screens;

import com.Arsanesia.TempleRush.TempleRush;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.BannerListener;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.OkButtonListener;
import com.Arsanesia.TempleRush.Enumeration.GeenieEnumeration;
import com.Arsanesia.TempleRush.Models.Profile;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;

public class AchievementScreen extends AbstractScreen{
	private final float TOP_MARGIN=95;
	private final float BOTTOM_MARGIN=17;
	private final float ELEMENTS_BOTTOM_MARGIN = 10;
	private final float CHECK_RIGHT_MARGIN = 32;
	private final float CHECK_BOTTOM_MARGIN = 42 ;
	private final float SCROLLPANE_ADJUSTMENT = TOP_MARGIN+BOTTOM_MARGIN;
	private final String INACTIVE = "BW";
	private Image background;
	private ScrollPane scrollpane;
	private Table table;
	private Array<Group> achievementsgroup;
	private Profile profile;
	private int achievementCount;
	public AchievementScreen(TempleRush game) {
		super(game);
		achievementsgroup = new Array<Group>();
		profile = game.getProfileManager().retrieveProfile();
		achievementCount = 0;
	}
	
	@Override
	public void show(){		

		atlas = new TextureAtlas(Gdx.files.internal("Images/Achievements/achievements.atlas"));
		background = new Image(new TextureRegionDrawable(new TextureRegion(atlas.createSprite("background"))));
		table = new Table();
		TextureRegion checkregion = new TextureRegion(atlas.createSprite("check"));
		//create okbutton
		Button okbutton = new Button(new TextureRegionDrawable(new TextureRegion(atlas.createSprite("ok"))));
		okbutton.addListener(new OkButtonListener(game));
		okbutton.setX(VIEWPORT_DEFAULTWIDTH-okbutton.getWidth());
		
		scrollpane = new ScrollPane(table);
		scrollpane.setWidth(VIEWPORT_DEFAULTWIDTH);
		scrollpane.setHeight(VIEWPORT_DEFAULTHEIGHT-SCROLLPANE_ADJUSTMENT);
		scrollpane.setY(BOTTOM_MARGIN);
		scrollpane.setScrollingDisabled(true, false);
		Image checkimage = null;
		boolean checked= false;
		Group group;
		Image elementimage;
		
		
		//insert geenie achievements
		for(GeenieEnumeration geenietype : GeenieEnumeration.values()){
			String geenietypename = geenietype.name();
			checked = false;
			if(geenietype.ordinal()< GeenieEnumeration.WHITE.ordinal() || 
					geenietype.equals(GeenieEnumeration.GOLD))
			{
				table.row().spaceBottom(ELEMENTS_BOTTOM_MARGIN);
				
				group = new Group();
				elementimage = new Image(new TextureRegionDrawable(new TextureRegion(atlas.createSprite(geenietypename))));
				
				if(profile.isGeenieAchievementCompleted(geenietypename))
				{
					checkimage = new Image(new TextureRegionDrawable(new TextureRegion(checkregion)));
					checkimage.setPosition(elementimage.getX()+elementimage.getWidth()-CHECK_RIGHT_MARGIN, elementimage.getY()+CHECK_BOTTOM_MARGIN);
					checked = true;
					achievementCount++;
				}
				else
				{
					elementimage = new Image(new TextureRegionDrawable(new TextureRegion(atlas.createSprite(INACTIVE+geenietypename))));
				}
				group.addActor(elementimage);
				if(checked) group.addActor(checkimage);
				group.setSize(elementimage.getWidth(), elementimage.getHeight());
				
				achievementsgroup.add(group);
				table.add(achievementsgroup.get(achievementsgroup.size-1));
			}
		}
		Array<Sprite> sprites = atlas.createSprites("TAPPER");
		int spritelength = sprites.size;
		int spriteindex=0;
		//insert tapper achievements
		for(spriteindex =0;spriteindex<(spritelength/2); spriteindex++ ){
			table.row().spaceBottom(ELEMENTS_BOTTOM_MARGIN);
			
			group = new Group();
			elementimage = new Image(new TextureRegionDrawable(new TextureRegion(sprites.get(spriteindex))));
			checked = false;
			if(profile.isTapAchievementCompleted(spriteindex))
			{
				checkimage = new Image(new TextureRegionDrawable(new TextureRegion(checkregion)));
				checkimage.setPosition(elementimage.getX()+elementimage.getWidth()-CHECK_RIGHT_MARGIN, elementimage.getY()+CHECK_BOTTOM_MARGIN);
				checked = true;
				achievementCount++;
			}
			else
			{
				elementimage = new Image(new TextureRegionDrawable(new TextureRegion(sprites.get(spriteindex+3))));
			}
			group.addActor(elementimage);
			if(checked) group.addActor(checkimage);
			group.setSize(elementimage.getWidth(), elementimage.getHeight());
			
			achievementsgroup.add(group);
			table.add(achievementsgroup.get(achievementsgroup.size-1));
		}
		
		//insert temple achievements
		sprites = atlas.createSprites("TEMPLE");
		spritelength = sprites.size;
		spriteindex=0;
		for(spriteindex =0;spriteindex<(spritelength/2); spriteindex++){
			table.row().spaceBottom(ELEMENTS_BOTTOM_MARGIN);
			
			group = new Group();
			elementimage = new Image(new TextureRegionDrawable(new TextureRegion(sprites.get(spriteindex))));
			checked = false;
			
			if(profile.isTempleAchievementCompleted(spriteindex))
			{
				checkimage = new Image(new TextureRegionDrawable(new TextureRegion(checkregion)));
				checkimage.setPosition(elementimage.getX()+elementimage.getWidth()-CHECK_RIGHT_MARGIN, elementimage.getY()+CHECK_BOTTOM_MARGIN);
				checked = true;
				achievementCount++;
			}
			else
			{
				elementimage = new Image(new TextureRegionDrawable(new TextureRegion(sprites.get(spriteindex+3))));
			}
			
			group.addActor(elementimage);
			if(checked)group.addActor(checkimage);
			group.setSize(elementimage.getWidth(), elementimage.getHeight());

			achievementsgroup.add(group);
			table.add(achievementsgroup.get(achievementsgroup.size-1));
		}
		
		if(achievementCount>=3)
		{
			//create project claw banner image
			table.row().spaceBottom(ELEMENTS_BOTTOM_MARGIN);
			group = new Group();
			elementimage = new Image(new TextureRegionDrawable(new TextureRegion(atlas.createSprite("banner"))));
			//set listener to open uri
			elementimage.addListener(new BannerListener(game));
			group.setSize(elementimage.getWidth(), elementimage.getHeight());
			group.addActor(elementimage);
			//add it to the first achievements
			achievementsgroup.add(group);
//			table.addActorAt(0, elementimage);
			table.add(elementimage);
		}
		
		
		//add background to stage
		this.addActor(background);
		//add scrollpane to stage
		this.addActor(scrollpane);
		this.addActor(okbutton);
		
	}

}
